import Vue from 'vue'
import VueRouter from 'vue-router'

import routerfzy from "./modules/fzy";
import routerhhp from "./modules/hhp";
import routerrrk from "./modules/rrk";
import routerlag from "./modules/lag";
import routerpm from "./modules/pm";
import routerwyn from "./modules/wyn";
import routerjpf from "./modules/jpf";


Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',

    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    children: [
      ...routerfzy,
      ...routerhhp,
      ...routerrrk,
      ...routerlag,
      ...routerpm,
      ...routerwyn,
      ...routerjpf,
    ]
  },
  {
    path: "/",
    redirect: "/home/shouye"
  },
  {
    path: '*',
    name: 'no',
    component: () => import('../views/NoView.vue')
  },
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})

// 全局路由前置守卫
router.beforeEach((to, from, next) => {
  // 从页面存储判断是否有token
  const token = sessionStorage.getItem('token')
  // 如果没有token，且访问的不是登录页，就跳转到登录页
  if (!token && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }

})


export default router
